package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsDocumentBean;
import com.imodia.checkoo.bean.CmsDocumentInfoBean;
import com.imodia.checkoo.service.impl.CmsDocumentInfoServiceImpl;
import com.imodia.checkoo.service.impl.CmsDocumentServiceImpl;



@Controller
@RequestMapping("/hfmanager/document/list.do")
public class CmsDocumentController {
	
	private final Log log = LogFactory.getLog(CmsDocumentController.class);
	
	private final String baseUrl = "document";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit_redirect = "/hfmanager/" +baseUrl + "/list.do?action=edit";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";

	@Autowired
	private CmsDocumentServiceImpl cmsDocumentService;
	@Autowired
	private CmsDocumentInfoServiceImpl cmsDocumentInfoService;
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsDocumentService.delete(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsDocumentBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsDocumentService.update(obj);
		}else{
			x = cmsDocumentService.insert(obj);
		}
		return new ModelAndView(new RedirectView(request.getContextPath()+url_edit_redirect+"&id="+obj.getId()+"&x="+x), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();
		CmsDocumentBean obj = cmsDocumentService.get(id);

		if(obj!=null){
			SqlParam sql = new SqlParam();
			sql.andEqualTo("document_id", String.valueOf(obj.getId()));
			sql.andOrderListValue("priority", "");
			if(obj.getObjectName()!=null && obj.getObjectName().trim().length()>0 && cmsDocumentInfoService.isExistTable(obj.getObjectName().trim())>0){
				
				PageResult<CmsDocumentInfoBean> pageResult =cmsDocumentInfoService.page(sql,new PageBean());
				m.put("page", pageResult);
				
				m.put("extField", cmsDocumentInfoService.getTableField(obj.getObjectName()));
			}
		}
		if(obj==null) obj = new CmsDocumentBean();
			m.put("info", obj);
			
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsDocumentBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		SqlParam sql = new SqlParam();
		sql.andOrderListValue("priority", "");
	
		PageResult<CmsDocumentBean> pageResult =cmsDocumentService.page(sql,page);
		model.put("page", pageResult);
		return new ModelAndView(url_list, model);
	}

}
